import '../css/global.css'
import './polyfill'
function detectZoom() {
    var ratio = 0,
        screen = window.screen,
        ua = navigator.userAgent.toLowerCase();

    if (window.devicePixelRatio !== undefined) {
        ratio = window.devicePixelRatio;
    }
    else if (~ua.indexOf('msie')) {
        if (screen.deviceXDPI && screen.logicalXDPI) {
            ratio = screen.deviceXDPI / screen.logicalXDPI;
        }
    }
    else if (window.outerWidth !== undefined && window.innerWidth !== undefined) {
        ratio = window.outerWidth / window.innerWidth;
    }

    if (ratio) {
        ratio = Math.round(ratio * 100);
    }
    return ratio;
}

function doubleTouch() {
    var lastTouchEnd = 0;
    document.addEventListener('touchend', function (event) {
        var now = (new Date()).getTime();
        if (now - lastTouchEnd <= 300) {
            event.preventDefault();
        }
        lastTouchEnd = now;
    }, false)
}
window.onload = doubleTouch


let u = navigator.userAgent;

//android终端
let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1 ||u.indexOf('Linux')>-1||u.indexOf('HUAWEI')>-1 

//ios终端
let isIos = u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/) ? false : true 

let isPc = u.match(/(iPhone|iPad|iPod|Android|ios)/i) ? false : true
//android终端 //低端手机
let isLod = (u.indexOf('Android') > -1 || u.indexOf('Adr') > -1 || u.indexOf('Linux') > -1 || u.indexOf('HUAWEI') > -1 || u.indexOf('Windows NT 6.1') > -1) ? true : false

let W = document.documentElement.clientWidth
let H = document.documentElement.clientHeight

let config = {
    w: W,
    h: H,
    px: function (value, px = 750) { return value * (W / px) },
    isAndroid :isAndroid,
    isIos: isIos,
    isPc: isPc,
    isLod: isLod,
    formatDate: (date, format) => {
        let paddNum = function (num) {
            num += "";
            return num.replace(/^(\d)$/, "0$1");
        }
        //指定格式字符
        let cfg = {
            yyyy: date.getFullYear() //年 : 4位
            , yy: date.getFullYear().toString().substring(2)//年 : 2位
            , M: date.getMonth() + 1  //月 : 如果1位的时候不补0
            , MM: paddNum(date.getMonth() + 1) //月 : 如果1位的时候补0
            , d: date.getDate()   //日 : 如果1位的时候不补0
            , dd: paddNum(date.getDate())//日 : 如果1位的时候补0
            , hh: date.getHours()  //时
            , mm: date.getMinutes() //分
            , ss: date.getSeconds() //秒
        }
        format || (format = "yyyy-MM-dd hh:mm:ss");
        return format.replace(/([a-z])(\1)*/ig, function (m) { return cfg[m]; })
    },
    checkName: (value) => {
        return value.length < 12
    },
    checkSampleCode:(value)=>{
        let reg  = new RegExp(/^SPS[0-9]{8}$/);
        let reg1 = new RegExp(/^\d{2}T\d{7}$/);
        if(reg.test(value)){
            return reg.test(value);
        }else if(reg1.test(value)){
            return reg1.test(value);
        }
        return false;
    },
    checkPassword: (value) => {
        let lower = /[a-z]/
        let upper =  /[A-Z]/
        let digit = /[0-9]/
        if(value.length>=6&&lower.test(value)&&upper.test(value)&&digit.test(value)){
            return true;
        }
        return  false;
    },
    checkIDcard: (num) => {
        //if (isNaN(num)) {alert("输入的不是数字！"); return false;} 
        num = num.replace(/-/g, '')
        var len = num.length, re;
        if (len === 15)
            re = new RegExp(/^(\d{6})()?(\d{2})(\d{2})(\d{2})(\d{2})(\w)$/);
        else if (len == 18)
            re = new RegExp(/^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})(\w)$/);
        else {
            //alert("输入的数字位数不对。"); 
            return false;
        }
        var a = num.match(re);
        if (a != null) {
            if (len === 15) {
                var D = new Date("19" + a[3] + "/" + a[4] + "/" + a[5]);
                var B = D.getYear() === a[3] && (D.getMonth() + 1) === a[4] && D.getDate() == a[5];
            }
            else {
                var D = new Date(a[3] + "/" + a[4] + "/" + a[5]);
                var B = D.getFullYear() === a[3] && (D.getMonth() + 1) === a[4] && D.getDate() === a[5];
            }
            if (!B) {
                //alert("输入的身份证号 "+ a[0] +" 里出生日期不对。"); 
                return false;
            }
        }
        if (!re.test(num)) {
            //alert("身份证最后一位只能是数字和字母。");
            return false;
        }
        return true;
    },
    checkPhoneNumber: (value) => {
        value = value.replace(/-/g, '')
        return value.length === 11 && /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$/.test(value)
    }
}
export default config